package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.*;
import com.cssl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
public class BaikeController {

    @Autowired
    private PetinfoService service;

    @Autowired
    private TrainingService service1;

    @Autowired
    private MedicalService service2;
    @Autowired
    private PregnancyService service3;

    @Autowired
    private NursingService service4;


    @Autowired
    private DiseaseService service5;

    @Autowired
    private NewsService service6;

    @Autowired
    private ScoreService service7;

    @Resource
    private MongoTemplate template;

    //宠物介绍
    @RequestMapping("/petdesc")
    public IPage<Petinfo> petdesc(@RequestParam Map<String,Object> map){
       LambdaQueryWrapper<Petinfo> lqw = Wrappers.lambdaQuery();
       lqw.like(Petinfo::getBname,map.get("bname").toString());
       int type =Integer.parseInt(map.get("type").toString());
       if (type != 0){
           lqw.eq(Petinfo::getBtype,type);
       }
       lqw.orderByAsc(Petinfo::getBid);
       return service.page(new Page<>(Integer.parseInt(map.get("index").toString()),13),lqw);
    }
    @RequestMapping("/petdesc1")
    public IPage<Petinfo> petdesc1(@RequestParam Map<String,Object> map){
        LambdaQueryWrapper<Petinfo> lqw = Wrappers.lambdaQuery();
        lqw.like(Petinfo::getBname,map.get("bname").toString());
        int type =Integer.parseInt(map.get("type").toString());
        if (type != 0){
            lqw.eq(Petinfo::getBtype,type);
        }
        lqw.orderByAsc(Petinfo::getBid);
        return service.page(new Page<>(Integer.parseInt(map.get("index").toString()),9),lqw);
    }
    //宠物训练
    @RequestMapping("/petxl")
    public Map<String,Object> petxl(@RequestParam Map<String,Object> map){
        Map<String,Object> map1 = new HashMap<>();

        List<Training> list = template.findAll(Training.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service1.list(),Training.class);
        }
        Query query = new Query();
        //起始下标
        int index = Integer.parseInt(map.get("index").toString());
        //宠物类型
        int type =Integer.parseInt(map.get("type").toString());

        Pattern pattern = Pattern.compile("^.*"+map.get("bname").toString()+".*$",Pattern.CASE_INSENSITIVE);
        Criteria cra = Criteria.where("traintitle").regex(pattern);
        if (type != 0){
            cra.andOperator(Criteria.where("types").is(type));
        }
        query.addCriteria(cra);
        //总记录
        Long count = template.count(query,Training.class);
        query.with(PageRequest.of(index-1,6));//, Sort.Direction.DESC,"deptno"
        map1.put("index",index);//当前页码
        map1.put("count",count);//总记录
        map1.put("pages",count%6==0?count/6:count/6+1);//总页数
        List<Training> list2 = template.find(query,Training.class);
        System.out.println("mongo xl list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //宠物训练2
    @RequestMapping("/petxl2")
    public Map<String,Object> petxl2(){
        Map<String,Object> map1 = new HashMap<>();

        List<Training> list = template.findAll(Training.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service1.list(),Training.class);
        }
        Query query = new Query();
        query.with(PageRequest.of(0,10,Sort.Direction.DESC,"tdate"));//, Sort.Direction.DESC,"deptno"
        List<Training> list2 = template.find(query,Training.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //宠物医疗
    @RequestMapping("/petyl")
    public Map<String,Object> petyl(@RequestParam Map<String,Object> map){
        Map<String,Object> map1 = new HashMap<>();
        System.out.println("yl map:"+map);
        List<Medical> list = template.findAll(Medical.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service2.list(),Medical.class);
        }
        Query query = new Query();
        //起始下标
        int index = Integer.parseInt(map.get("index").toString());
        //宠物类型
        int type =Integer.parseInt(map.get("type").toString());

        Pattern pattern = Pattern.compile("^.*"+map.get("bname").toString()+".*$",Pattern.CASE_INSENSITIVE);
        Criteria cra = Criteria.where("mtitle").regex(pattern);
        if (type != 0){
            cra.andOperator(Criteria.where("mtypes").is(type));
        }
        query.addCriteria(cra);
        //总记录
        Long count = template.count(query,Medical.class);
        query.with(PageRequest.of(index-1,6));//, Sort.Direction.DESC,"deptno"
        map1.put("index",index);//当前页码
        map1.put("count",count);//总记录
        map1.put("pages",count%6==0?count/6:count/6+1);//总页数
        List<Medical> list2 = template.find(query,Medical.class);
        System.out.println("mongo yl list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //宠物医疗2
    @RequestMapping("/petyl2")
    public  Map<String,Object> petyl2(){
        Map<String,Object> map1 = new HashMap<>();
        List<Medical> list = template.findAll(Medical.class);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service2.list(),Medical.class);
        }
        Query query = new Query();
        query.with(PageRequest.of(0,10,Sort.Direction.DESC,"mdate"));
        List<Medical> list2 = template.find(query,Medical.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //宠物孕事
    @RequestMapping("/petys")
    public Map<String,Object> petys(@RequestParam Map<String,Object> map){

        System.out.println("ys map:"+map);
        Map<String,Object> map1 = new HashMap<>();
        List<Pregnancy> list = template.findAll(Pregnancy.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service3.list(),Pregnancy.class);
        }

        Query query = new Query();
        //起始下标
        int index = Integer.parseInt(map.get("index").toString());
        //宠物类型
        int type =Integer.parseInt(map.get("type").toString());
        Pattern pattern = Pattern.compile("^.*"+map.get("bname").toString()+".*$",Pattern.CASE_INSENSITIVE);

        Criteria cra = Criteria.where("gtitle").regex(pattern);
        if (type != 0){
            cra.andOperator(Criteria.where("gtypes").is(type));
        }
//        query.addCriteria(cra);
        //总记录
        Long count = template.count(query,Pregnancy.class);
        query.with(PageRequest.of(index-1,6));
        map1.put("index",index);//当前页码
        map1.put("count",count);//总记录
        map1.put("pages",count%6==0?count/6:count/6+1);//总页数
        List<Pregnancy> list2 = template.find(query,Pregnancy.class);
        System.out.println("mongo ys list2:"+list2);
        map1.put("list",list2);
        return map1;
    }

    //宠物孕事2
    @RequestMapping("/petys2")
    public Map<String,Object> petys2(){
        Map<String,Object> map1 = new HashMap<>();
        List<Pregnancy> list = template.findAll(Pregnancy.class);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service3.list(),Pregnancy.class);
        }
        Query query = new Query();
        query.with(PageRequest.of(0,10,Sort.Direction.DESC,"gdate"));
        List<Pregnancy> list2 = template.find(query,Pregnancy.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }

    //护理
    @RequestMapping("/pethl")
    public Map<String,Object> pethl(@RequestParam Map<String,Object> map){
        Map<String,Object> map1 = new HashMap<>();

        List<Nursing> list = template.findAll(Nursing.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service4.list(),Nursing.class);
        }
        Query query = new Query();
        //起始下标
        int index = Integer.parseInt(map.get("index").toString());
        //宠物类型
        int type =Integer.parseInt(map.get("type").toString());

        Pattern pattern = Pattern.compile("^.*"+map.get("bname").toString()+".*$",Pattern.CASE_INSENSITIVE);
        Criteria cra = Criteria.where("ntitle").regex(pattern);
        if (type != 0){
            cra.andOperator(Criteria.where("ntype").is(type));
        }
        query.addCriteria(cra);
        //总记录
        Long count = template.count(query,Nursing.class);
        query.with(PageRequest.of(index-1,6));//, Sort.Direction.DESC,"deptno"
        map1.put("index",index);//当前页码
        map1.put("count",count);//总记录
        map1.put("pages",count%6==0?count/6:count/6+1);//总页数
        List<Nursing> list2 = template.find(query,Nursing.class);
        System.out.println("mongo hl list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //护理2
    @RequestMapping("/pethl2")
    public Map<String,Object> pethl2(){
        Map<String,Object> map1 = new HashMap<>();
        List<Nursing> list = template.findAll(Nursing.class);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service4.list(),Nursing.class);
        }
        Query query = new Query();
        query.with(PageRequest.of(0,10,Sort.Direction.DESC,"ndate"));
        List<Nursing> list2 = template.find(query,Nursing.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }

    //病症
    @RequestMapping("/petbz")
    public Map<String,Object> petbz(@RequestParam Map<String,Object> map){
        Map<String,Object> map1 = new HashMap<>();
        List<Disease> list = template.findAll(Disease.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service5.list(),Disease.class);
        }
        Query query = new Query();
        //起始下标
        int index = Integer.parseInt(map.get("index").toString());
        //宠物类型
        int type =Integer.parseInt(map.get("type").toString());

        Pattern pattern = Pattern.compile("^.*"+map.get("bname").toString()+".*$",Pattern.CASE_INSENSITIVE);
        Criteria cra = Criteria.where("distitle").regex(pattern);
        if (type != 0){
            cra.andOperator(Criteria.where("distype").is(type));
        }
        query.addCriteria(cra);
        //总记录
        Long count = template.count(query,Disease.class);
        query.with(PageRequest.of(index-1,6));//, Sort.Direction.DESC,"deptno"
        map1.put("index",index);//当前页码
        map1.put("count",count);//总记录
        map1.put("pages",count%6==0?count/6:count/6+1);//总页数
        List<Disease> list2 = template.find(query,Disease.class);
        System.out.println("mongo bz list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //病症2
    @RequestMapping("/petbz2")
    public Map<String,Object> petbz2(){
        Map<String,Object> map1 = new HashMap<>();
        List<Disease> list = template.findAll(Disease.class);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service5.list(),Disease.class);
        }
        Query query = new Query();
        query.with(PageRequest.of(0,10,Sort.Direction.DESC,"disdate"));
        List<Disease> list2 = template.find(query,Disease.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }

    //新闻
    @RequestMapping("/petnews")
    public Map<String,Object> petnew(@RequestParam Map<String,Object> map){
        Map<String,Object> map1 = new HashMap<>();
        List<News> list = template.findAll(News.class);
        System.out.println("mongo list:"+list);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service6.list(),News.class);
        }
        Query query = new Query();
        //起始下标
        int index = Integer.parseInt(map.get("index").toString());
        Pattern pattern = Pattern.compile("^.*"+map.get("bname").toString()+".*$",Pattern.CASE_INSENSITIVE);
        Criteria cra = Criteria.where("newtitle").regex(pattern);
        query.addCriteria(cra);
        //总记录
        Long count = template.count(query,News.class);
        query.with(PageRequest.of(index-1,6));//, Sort.Direction.DESC,"deptno"
        map1.put("index",index);//当前页码
        map1.put("count",count);//总记录
        map1.put("pages",count%6==0?count/6:count/6+1);//总页数
        List<News> list2 = template.find(query,News.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }
    //新闻2
    @RequestMapping("/petnews2")
    public Map<String,Object> petnew2(){
        Map<String,Object> map1 = new HashMap<>();
        List<News> list = template.findAll(News.class);
        if (list.size() == 0 || list == null){
            //添加
            template.insert(service6.list(),Disease.class);
        }
        Query query = new Query();
        query.with(PageRequest.of(0,10,Sort.Direction.DESC,"newdate"));
        List<News> list2 = template.find(query,News.class);
        System.out.println("mongo list2:"+list2);
        map1.put("list",list2);
        return map1;
    }

    //显示单个品种评分
    @RequestMapping("/petscore")
    public Score petscore(@RequestParam Integer bid){
        LambdaQueryWrapper<Score> lqw = Wrappers.lambdaQuery();
        lqw.eq(Score::getB_id,bid);
        return service7.getOne(lqw);
    }

    //显示推荐
    @RequestMapping("/tjpetinfo")
    public IPage<Petinfo> tjpetinfo(@RequestParam Map<String,Object> map){
        LambdaQueryWrapper<Petinfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(Petinfo::getBtype,Integer.parseInt(map.get("types").toString()));
        return service.page(new Page<>(0,3),lqw);
    }


}
